#include "fontlistdelegate.h"
#include <QPainter>
#include <QStyleOptionViewItem>
#include <QModelIndex>
#include <QTextStream>
#include <QPainterPath>
#include <QFontDatabase>

#include "fontlistmodel.h"

FontListDelegate::FontListDelegate(QObject *parent) : QStyledItemDelegate(parent)
{
    
}

void FontListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
//    QTextStream(stdout) << index.data(FontListModel::FontViewRole::ContentFontRole).toString() << "\n";
    auto fontId = index.data(FontListModel::FontIdRole).toInt();
    auto fontFamily = index.data(FontListModel::FontFamilyRole).toString();
    auto fontWeight = index.data(FontListModel::FontWeightRole).toInt();
//    QTextStream(stdout) << QString(">> families :%1, italic: %2，styleName: %3, styles: %4\n")
//                           .arg("1")
//                           .arg(QFontDatabase().italic(family,"abd"))
//                           .arg(QFontDatabase().styleString(family))
//                           .arg(QFontDatabase().styles(family).join(","));

    painter->save();
    painter->setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);
    QPen oldPen = painter->pen();

// 控件区域
    QRectF rect = option.rect;

// 画一个矩形
    const qreal radius = 15;
    QPainterPath path;

    // 此处是圆角的算法(x,y)
    QPointF rl(0, radius);
    QPointF nrl(0, -radius);
    QPointF lr(radius, 0);
    QPointF nlr(-radius,0);

    QPointF rtr = rect.topRight() - QPoint(1,0);
    QPointF rtl = rect.topLeft();
    QPointF rbl = rect.bottomLeft() - QPoint(0,1);
    QPointF rbr = rect.bottomRight() - QPoint(1,1);

    path.moveTo(rtr - lr);          // rtr:rect的顶部右边, 减去 lr(7,0)
    path.lineTo(rtl + lr);          // rtl:rect的顶部左边，加上 lr(7,0)
    path.quadTo(rtl, rtl + rl);     // rtl:rect的顶部左边，y轴相同，加上 rl(0, 7); 绘制一个贝赛尔曲线
    path.lineTo(rbl + nrl);         // rbl:rect的底部左边，加上 nlr(-7,0)
    path.quadTo(rbl, rbl + lr);     // rbl:rect的底部左边，加上 lr(7,0)
    path.lineTo(rbr - lr);          // rbr:rect的底部右边，减去 lr(7,0)
    path.quadTo(rbr, rbr + nrl);    // rbr:rect的底部右边，y轴相同，加上 nrl(0, -7)
    path.lineTo(rtr + rl);          // rtr:rect的顶部右边，加上 rl(0,7)
    path.quadTo(rtr, rtr + nlr);    // rtr:rect的顶部右边，y轴相同，加上 nlr(-7,0)

    painter->setPen(QPen(Qt::black));
    painter->fillPath(path, QBrush(QColor("#F4F4F6")));     // 绘制一小块地

    QFont newFont(fontFamily);

    // 字体名称
    QRectF rect_title = QRectF(rect.left() + 10, rect.top(), rect.width(), rect.height());
    QString title = index.data(FontListModel::FontFamilyRole).toString();
    painter->drawText(rect_title, title);
    painter->setFont(newFont);

    // 中文说明
    QString cn;// = index.data(FontListModel::ContentCNRole).toString();
    cn = "你们这些家伙，就不会写个字体预览工具?";
    int width_cn = painter->fontMetrics().horizontalAdvance(cn);
    QRectF rect_cn = QRectF(rect.left() + ((rect.width() - width_cn)/2), rect.top() + 24, rect.width(), rect.height());
    painter->drawText(rect_cn, cn);

    // 英文说明
    QString en;// = index.data(FontListModel::ContentENRole).toString();
    en = "You guys, you don't write a font preview tool?";
    en = painter->fontMetrics().elidedText(en, Qt::TextElideMode::ElideRight, rect.width() - 40);
    int width_en = painter->fontMetrics().horizontalAdvance(en);

    QRectF rect_en = QRectF(rect.left() + ((rect.width() - width_en)/2), rect.top() + 48, rect.width(), rect.height());
    painter->drawText(rect_en, en);

    painter->restore();
}

QSize FontListDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
//    return index.data(Qt::SizeHintRole).toSize();
    return QSize(320, 80);
}
